{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4453cad3-8008-44a6-9eae-67b757689b09",
   "metadata": {},
   "source": [
    "map和geo的区别（来自文心一言）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2852e43-3a92-4ca7-9149-82ea5cda3a86",
   "metadata": {},
   "source": [
    "一、定义与基本概念"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af1c18a9-f57f-493e-ba1e-22ea450a4577",
   "metadata": {},
   "source": [
    "Map：\r\n",
    "Map是Pyecharts中专门用于绘制地图的图表类型。\r\n",
    "它能够清晰地展示地理数据的分布和对比情况，常用于全国或全球范围内的数据可视化。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb44ead6-51b8-4c5f-8deb-12826f2b3c1a",
   "metadata": {},
   "source": [
    "Geo：\r\n",
    "Geo是Pyecharts中用于地理坐标数据可视化的图表类型，相比Map提供了更丰富的绘制选项。\r\n",
    "Geo不仅支持地图的绘制，还可以绘制散点图、热力图等多种地理图表类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "262433cd-26ab-4807-9e03-06ddcf74a0d7",
   "metadata": {},
   "source": [
    "二、数据结构与展示方式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8ffed97-433e-4212-a7b5-ce6f2d6fd48c",
   "metadata": {},
   "source": [
    "Map：\r\n",
    "Map的数据结构通常是一个包含地区名称和数据值的列表或字典。\r\n",
    "地图上的每个地区（如省份、城市、国家等）都会被赋予一个数据值，并通过颜色或大小的变化来表示数据的差异。\r\n",
    "Map提供了多种地图类型选项，如中国地图、世界地图等，并支持自定义地图的样式和配置。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f33e7787-4d9f-4a3e-a219-4fa0871c5922",
   "metadata": {},
   "source": [
    "Geo：\r\n",
    "Geo的数据结构相对复杂一些，它支持多种数据格式，包括地理坐标点、地理区域和数据值等。\r\n",
    "散点图：Geo可以通过散点图的方式展示地理数据，每个散点代表一个地理坐标点，并可以通过颜色、大小等属性来表示数据值。\r\n",
    "热力图：Geo还支持热力图的绘制，通过颜色的深浅来表示数据的密集程度或强度。\r\n",
    "自定义绘制：Geo提供了高度的自定义功能，用户可以在地图上添加自定义的线条、标记等元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c651421f-cd20-4f7f-be08-133250162c7d",
   "metadata": {},
   "source": [
    "三、适用场景与灵活性"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f9a2762-0131-41b4-a312-9e60dc303602",
   "metadata": {},
   "source": [
    "Map：\r\n",
    "适用于展示全国或全球范围内的数据分布和对比情况。\r\n",
    "由于其数据结构相对简单且直观，适合用于数据丰富的场景。\r\n",
    "Map提供了多种预设的地图类型和样式选项，方便用户快速上手和配置。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01725485-2590-4f87-ba66-427f3b57915a",
   "metadata": {},
   "source": [
    "Geo：\r\n",
    "适用于展示小数据集或需要在地图上添加自定义元素的场景。\r\n",
    "Geo提供了更高的灵活性，支持多种地理图表类型和自定义绘制功能。\r\n",
    "用户可以根据具体需求和数据特点，自定义地图的样式、颜色、线条等元素，以实现更个性化的数据可视化效果。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b953c51c-abec-4938-8cb0-06500dc8500d",
   "metadata": {},
   "source": [
    "【1】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2807518-541f-49fe-9dbb-c553bdd76fe2",
   "metadata": {},
   "source": [
    "MAP系列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9aaf253-098b-4d57-b857-73af91b7f79a",
   "metadata": {},
   "source": [
    "1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7e54cb5-d606-49d0-a5f7-e927acd9aeba",
   "metadata": {},
   "source": [
    "中国地图精确到省"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f5c8c474-8d63-458b-b5c3-6b2a23fc370a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['湖北', 68160, 63647, 4512], ['台湾', 14080, 7534, 569], ['香港', 11889, 11616, 210], ['广东', 2706, 2461, 8], ['上海', 2184, 2109, 7], ['黑龙江', 1612, 1599, 13], ['浙江', 1383, 1334, 1], ['河北', 1317, 1310, 7], ['河南', 1317, 1291, 22], ['北京', 1075, 1051, 9], ['四川', 1064, 1032, 3], ['湖南', 1051, 1046, 4], ['安徽', 1004, 998, 6], ['新疆', 980, 977, 3], ['江西', 937, 936, 1], ['山东', 883, 875, 7], ['江苏', 740, 726, 0], ['福建', 659, 612, 1], ['陕西', 624, 608, 3], ['重庆', 598, 589, 6], ['吉林', 573, 570, 3], ['辽宁', 426, 421, 2], ['天津', 399, 385, 3], ['内蒙古', 394, 387, 1], ['云南', 391, 350, 2], ['广西', 275, 273, 2], ['山西', 253, 252, 0], ['甘肃', 194, 192, 2], ['海南', 188, 181, 6], ['贵州', 147, 145, 2], ['宁夏', 76, 75, 0], ['澳门', 53, 51, 0], ['青海', 18, 18, 0], ['西藏', 1, 1, 0]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\中国地图精确到省地图.html'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Map\n",
    "from pyecharts.globals import ThemeType\n",
    "import pyecharts.options as opts\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/china_province_history_data.xlsx')\n",
    "data['date'] = pd.to_datetime(data['date']) # 将数据转换为日期格式\n",
    "data.set_index('date',inplace=True)\n",
    "# 获取2021-06-21的数据，并按累计确诊人数排序\n",
    "data_pair = data.loc['2021-06-21',['省份','total_confirm','total_heal','total_dead']].sort_values(by='total_confirm',ascending=False).values.tolist()\n",
    "print(data_pair)\n",
    "# 确保省份名称与pyechaarts地图中的名称一致\n",
    "province_mapping = {\n",
    "    '台湾':'台湾省','香港':'香港特别行政区','澳门':'澳门特别行政区','安徽':'安徽省','福建':'福建省',\n",
    "    '甘肃':'甘肃省','广东': '广东省','广西': '广西省','贵州': '贵州省','海南': '海南省','河北': '河北省',\n",
    "    '河南': '河南省','黑龙江': '黑龙江省','湖北': '湖北省','湖南': '湖南省','吉林': '吉林省',\n",
    "    '江苏': '江苏省','江西': '江西省','辽宁': '辽宁省','内蒙古': '内蒙古自治区','宁夏': '宁夏省',\n",
    "    '青海': '青海省','山东': '山东省','山西': '山西省','陕西': '陕西省','上海': '上海','四川': '四川省',\n",
    "    '天津': '天津','西藏': '西藏自治区','新疆': '新疆维吾尔自治区','云南': '云南省','浙江': '浙江省',\n",
    "    '重庆': '重庆市'\n",
    "}\n",
    "# 映射省份名称\n",
    "data_pair = [[province_mapping.get(i[0],i[0]),i[1]] for i in data_pair]\n",
    "'''province_mapping.get(i[0], i[0]) 尝试从 province_mapping 字典中获取与 i[0] 对应的值。\n",
    "如果 i[0] 是字典中的一个键，则返回对应的值（即映射后的省份名称）。\n",
    "如果 i[0] 不是字典中的键，则返回 i[0] 本身（即原始省份名称）'''\n",
    "# 创建地图\n",
    "maps = (\n",
    "    Map(init_opts=opts.InitOpts(theme=ThemeType.ESSOS))\n",
    "    .add(\n",
    "        '累计确诊',\n",
    "        [[province_mapping.get(i[0],i[0]),i[1]] for i in data_pair],\n",
    "        'china',  # 指定pyecharts要操作的地图库是中国的地图库\n",
    "        is_map_symbol_show=True, # 确保显示标记\n",
    "        label_opts=opts.LabelOpts(is_show=True), # 显示省份名称\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        visualmap_opts=opts.VisualMapOpts(\n",
    "            is_piecewise=True, # 使用分段式视觉映射组件\n",
    "            pieces=[\n",
    "                {'min':1001,'color':'red'},# 可以带label标签{'min':1001,'label':'>1000','color':'red'}\n",
    "                {'min':1000,'max':999,'color':'#0764e5'},\n",
    "                {'min':500,'max':999,'color':'#ffcd2b'},\n",
    "                {'min':100,'max':499,'color':'#00c5d2'},\n",
    "                {'min':10,'max':99,'color':'#4e70f0'},\n",
    "                {'min':1,'max':9,'color':'#fd802d'},\n",
    "            ],\n",
    "            pos_left='10%',pos_top='60%'\n",
    "        ),\n",
    "        title_opts=opts.TitleOpts(\n",
    "            title='全国各省累计确诊人数地图',pos_left='center',\n",
    "            subtitle=f'当前确诊人数排名第二的区域为{data_pair[1][0]},确诊人数为{data_pair[1],[1]}',\n",
    "            subtitle_textstyle_opts=opts.TextStyleOpts(color='green')\n",
    "        ),\n",
    "        legend_opts=opts.LegendOpts(pos_top='10%')\n",
    "    )\n",
    ")\n",
    "maps.render('中国地图精确到省地图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec26b231-21d4-4ae1-9799-11c2ad873f33",
   "metadata": {},
   "source": [
    "2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d3741bf-e360-4045-8e47-668bbc5c8ed8",
   "metadata": {},
   "source": [
    "中国地图精确到市"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d4478b52-b616-4e4b-a141-dc8f1aed2d93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\2023年河南省GDP地图.html'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Map\n",
    "from pyecharts.globals import ThemeType\n",
    "import pyecharts.options as opts\n",
    "\n",
    "# 假设2023年河南省各市的GDP数据（单位：亿元）\n",
    "# 格式为[(城市名称：GDP)]\n",
    "data = [(\"郑州市\",12000),(\"开封市\",2400),(\"洛阳市\",5000),(\"平顶山市\",2200),\n",
    "       (\"安阳市\",2300),(\"鹤壁市\",1000),(\"新乡市\",2800),(\"焦作市\",2000),\n",
    "       (\"濮阳市\",1600),(\"许昌市\",3000),(\"漯河市\",2600),(\"周口市\",3100),\n",
    "       (\"南阳市\",4100),(\"商丘市\",2900),(\"信阳市\",2600),(\"三门峡市\",1500),\n",
    "       (\"驻马店市\",2500),(\"济源市\",800),]\n",
    "\n",
    "# 创建地图\n",
    "maps = (\n",
    "    Map(init_opts=opts.InitOpts(theme=ThemeType.ESSOS))\n",
    "    .add('GDP',data,\"河南\") # 指定maptype为河南\n",
    "    .set_global_opts(\n",
    "        visualmap_opts=opts.VisualMapOpts(\n",
    "            max_=20000,\n",
    "            is_piecewise=True, # 使用分段式视觉映射组件\n",
    "        ),\n",
    "        title_opts=opts.TitleOpts(title='2023年河南省GDP地图',pos_left='center'),\n",
    "        legend_opts=opts.LegendOpts(pos_top='10%')\n",
    "    )\n",
    ")\n",
    "maps.render('2023年河南省GDP地图.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4cae3ae9-5053-4044-8bc5-f5cf7eba902c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\陕西省夏天平均温度地图.html'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Map\n",
    "import pyecharts.options as opts\n",
    "\n",
    "data = [(\"西安市\",28),(\"宝鸡市\",24),(\"咸阳市\",27),(\"渭南市\",29),\n",
    "       (\"铜川市\",23),(\"延安市\",22),(\"榆林市\",25),(\"汉中市\",27),\n",
    "       (\"安康市\",28),(\"商洛市\",25)]\n",
    "maps = (\n",
    "    Map()\n",
    "    .add(\"平均温度\",data,\"陕西\")\n",
    "    .set_global_opts(\n",
    "        visualmap_opts=opts.VisualMapOpts(\n",
    "            max_=30,\n",
    "            min_=20,\n",
    "            is_piecewise=True # 使用分段式视觉映射组件\n",
    "        ),\n",
    "        title_opts=opts.TitleOpts(title='陕西省夏天平均温度地图',pos_left='center')\n",
    "    )\n",
    ")\n",
    "maps.render('陕西省夏天平均温度地图.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "abd16414-7ad6-440c-a0d7-bcfd95b0b001",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\世界地图.html'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Map\n",
    "import pyecharts.options as opts\n",
    "from pyecharts.faker import Faker\n",
    "\n",
    "c = (\n",
    "    Map()\n",
    "    .add(\"商家A\",[list(z) for z in zip(Faker.country,Faker.values())],\"world\")\n",
    "    .set_global_opts(\n",
    "        visualmap_opts=opts.VisualMapOpts(\n",
    "            max_=200,\n",
    "            is_piecewise=True # 使用分段式视觉映射组件\n",
    "        ),\n",
    "        title_opts=opts.TitleOpts(title='世界地图',pos_left='center')\n",
    "    )\n",
    ")\n",
    "c.render('世界地图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad0f80d6-29f0-47ec-9775-21c7c28dc161",
   "metadata": {},
   "source": [
    "【2】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd5393b1-f963-43c0-af82-9db279e15a37",
   "metadata": {},
   "source": [
    "GEO系列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99c1f694-c166-4941-a89d-9748982b0d41",
   "metadata": {},
   "source": [
    "1、"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e219580d-c524-4c6e-a505-1c9a2a5ddc22",
   "metadata": {},
   "source": [
    "GEO散点图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2d28e63b-a98b-47a3-a622-a4451c315524",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\Geo-基本示例.html'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Geo \n",
    "from pyecharts.globals import ChartType,SymbolType\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "from pyecharts.commons.utils import JsCode\n",
    "import json\n",
    "\n",
    "# 从word_data.txt中加载数据\n",
    "with open('./data/word_data.txt','r',encoding='utf-8') as f:\n",
    "    data_pair = json.loads(f.read())\n",
    "\n",
    "# 确保国家名称与坐标数据中的名称一致\n",
    "country_list = ['China','United States','Brazil','United Kingdom','Canada','Russia','India']\n",
    "data_pair = [[x,y] for x,y in data_pair if x in country_list]   # 定义坐标\n",
    "\n",
    "# 定义标签格式化函数\n",
    "fmt_js = \"\"\"function(params){\n",
    "return params.name + ':' + Number(params.value[2]);\n",
    "}\"\"\"\n",
    "\n",
    "# 从crood_fixed.json 加载坐标数据\n",
    "with open('./data/crood_fixed.json','r',encoding='utf-8') as f:\n",
    "    data_loc = json.loads(f.read())\n",
    "\n",
    "# 创建geo对象\n",
    "geo = (\n",
    "    Geo(init_opts=opts.InitOpts(theme='dark',width='1000px'),\n",
    "    is_ignore_nonexistent_coord=True)\n",
    "    .add_schema(\n",
    "        maptype=\"world\",\n",
    "        is_roam=False,\n",
    "        itemstyle_opts=opts.ItemStyleOpts(\n",
    "            area_color='#091632',  # 世界地图地图颜色\n",
    "            border_color='#1773c3', # 国家边缘的颜色\n",
    "            opacity=1 # 透明度\n",
    "        )\n",
    "    )\n",
    ")\n",
    "# 添加自定义坐标\n",
    "for k,v in data_loc.items():\n",
    "    geo.add_coordinate(k,v[0],v[1])\n",
    "\n",
    "# 添加涟漪散点效果\n",
    "geo.add(\"\",data_pair,type_=ChartType.EFFECT_SCATTER)\n",
    "\n",
    "# 设置系列选项\n",
    "geo.set_series_opts(\n",
    "    label_opts=opts.LabelOpts(\n",
    "        is_show=True,\n",
    "        position='right',\n",
    "        color='white',\n",
    "        font_size=12,\n",
    "        font_weight='bold',\n",
    "        formatter=JsCode(fmt_js)\n",
    "    )\n",
    ")\n",
    "# 设置全局选项\n",
    "geo.set_global_opts(\n",
    "    visualmap_opts=opts.VisualMapOpts(is_show=False),\n",
    "        title_opts=opts.TitleOpts(title='Geo-基本示例',pos_left='center')\n",
    ")\n",
    "geo.render('Geo-基本示例.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b7ab66e7-abfa-426c-9352-32b5bd92008a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\Geo-基本示例1.html'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Geo \n",
    "from pyecharts.globals import ChartType,SymbolType\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "from pyecharts.commons.utils import JsCode\n",
    "import json\n",
    "\n",
    "# 从word_data.txt中加载数据\n",
    "with open('./data/word_data.txt','r',encoding='utf-8') as f:\n",
    "    data_pair = json.loads(f.read())\n",
    "\n",
    "# 确保国家名称与坐标数据中的名称一致\n",
    "country_list = ['China','United States','Brazil','United Kingdom','Canada','Russia','India']\n",
    "data_pair = [[x,y] for x,y in data_pair if x in country_list]   # 定义坐标\n",
    "\n",
    "# 定义标签格式化函数\n",
    "fmt_js = \"\"\"function(params){\n",
    "return params.name + ':' + Number(params.value[2]);\n",
    "}\"\"\"\n",
    "\n",
    "# 从crood_fixed.json 加载坐标数据\n",
    "with open('./data/crood_fixed.json','r',encoding='utf-8') as f:\n",
    "    data_loc = json.loads(f.read())\n",
    "\n",
    "# 添加自定义坐标\n",
    "for k,v in data_loc.items():\n",
    "    geo.add_coordinate(k,v[0],v[1])\n",
    "\n",
    "# 创建geo对象\n",
    "geo = (\n",
    "    Geo(init_opts=opts.InitOpts(theme='dark',width='1000px'),\n",
    "    is_ignore_nonexistent_coord=True)\n",
    "    .add_schema(\n",
    "        maptype=\"world\",\n",
    "        is_roam=False,\n",
    "        itemstyle_opts=opts.ItemStyleOpts(\n",
    "            area_color='#091632',  # 世界地图地图颜色\n",
    "            border_color='#1773c3', # 国家边缘的颜色\n",
    "            opacity=1 # 透明度\n",
    "        )\n",
    "    )\n",
    "    .add(\"\",data_pair,type_=ChartType.EFFECT_SCATTER)\n",
    "    .set_series_opts(\n",
    "    label_opts=opts.LabelOpts(\n",
    "        is_show=True,\n",
    "        position='right',\n",
    "        color='white',\n",
    "        font_size=12,\n",
    "        font_weight='bold',\n",
    "        formatter=JsCode(fmt_js)\n",
    "    ))\n",
    "    .set_global_opts(\n",
    "    visualmap_opts=opts.VisualMapOpts(is_show=False),\n",
    "        title_opts=opts.TitleOpts(title='Geo-基本示例',pos_left='center'))\n",
    ")\n",
    "geo.render('Geo-基本示例1.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cb777a5-c82d-4c9b-ba9d-d6d42f1b270e",
   "metadata": {},
   "source": [
    "2、"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "672aab0e-ee19-4471-aba3-d0b8a598dade",
   "metadata": {},
   "source": [
    "Geo-Bar地图操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "807c63ef-8815-4013-b9b1-618e83a81c4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\geo-bar.html'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Geo,Bar,Grid \n",
    "from pyecharts.globals import ChartType,SymbolType\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "from pyecharts.commons.utils import JsCode\n",
    "import json\n",
    "\n",
    "# 从word_data.txt中加载数据\n",
    "with open('./data/word_data.txt','r',encoding='utf-8') as f:\n",
    "    data_pair = json.loads(f.read())\n",
    "\n",
    "# 确保国家名称与坐标数据中的名称一致\n",
    "country_list = ['China','United States','Brazil','United Kingdom','Canada','Russia','India']\n",
    "data_pair = [[x,y] for x,y in data_pair if x in country_list]   # 定义坐标\n",
    "\n",
    "# 定义标签格式化函数\n",
    "fmt_js = \"\"\"function(params){\n",
    "return params.name + ':' + Number(params.value[2]);\n",
    "}\"\"\"\n",
    "\n",
    "# 从crood_fixed.json 加载坐标数据\n",
    "with open('./data/crood_fixed.json','r',encoding='utf-8') as f:\n",
    "    data_loc = json.loads(f.read())\n",
    "\n",
    "# 创建geo对象\n",
    "geo = (\n",
    "    Geo(init_opts=opts.InitOpts(theme='dark',width='1000px'),\n",
    "    is_ignore_nonexistent_coord=True)\n",
    "    .add_schema(\n",
    "        maptype=\"world\",\n",
    "        is_roam=False,\n",
    "        itemstyle_opts=opts.ItemStyleOpts(\n",
    "            area_color='#091632',  # 世界地图地图颜色\n",
    "            border_color='#1773c3', # 国家边缘的颜色\n",
    "            opacity=1 # 透明度\n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "# 添加自定义坐标\n",
    "for k,v in data_loc.items():\n",
    "    geo.add_coordinate(k,v[0],v[1])\n",
    "\n",
    "# 添加涟漪散点效果\n",
    "geo.add(\"\",data_pair,type_=ChartType.EFFECT_SCATTER)\n",
    "\n",
    "# 设置系列选项\n",
    "geo.set_series_opts(\n",
    "    label_opts=opts.LabelOpts(\n",
    "        is_show=True,\n",
    "        position='right',\n",
    "        color='white',\n",
    "        font_size=12,\n",
    "        font_weight='bold',\n",
    "        formatter=JsCode(fmt_js)\n",
    "    )\n",
    ")\n",
    "\n",
    "# 设置全局选项\n",
    "geo.set_global_opts(\n",
    "    visualmap_opts=opts.VisualMapOpts(is_show=False),\n",
    "        title_opts=opts.TitleOpts(title='Geo-散点自定义左侧bar样式',pos_left='center')\n",
    ")\n",
    "\n",
    "# 创建柱状图对象\n",
    "bar = (\n",
    "    Bar()\n",
    "    .add_xaxis([i[0] for i in data_pair]) # 添加x轴数据（国家名称）\n",
    "    .add_yaxis(\n",
    "        \"\",\n",
    "        [i[1] for i in data_pair],\n",
    "        itemstyle_opts=opts.ItemStyleOpts(\n",
    "            border_color='#c03b46',\n",
    "            opacity=0.8\n",
    "        )\n",
    "    )\n",
    "    .set_series_opts(\n",
    "        label_opts=opts.LabelOpts(\n",
    "            position='insideLeft',\n",
    "            font_size=10,\n",
    "            font_weight='bold',\n",
    "            formatter='{b}:{c}'\n",
    "        )\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        xaxis_opts=opts.AxisOpts(is_show=False), # 不显示x轴\n",
    "        yaxis_opts=opts.AxisOpts(is_show=False),\n",
    "        title_opts=opts.TitleOpts(title='Top10', \n",
    "                                  pos_top='55%', # 标题距离顶部的百分比\n",
    "                                  pos_left='5%', # 标题距离左侧的百分比\n",
    "                                  title_textstyle_opts=opts.TextStyleOpts(font_size=12) # 标题文本样式配置项\n",
    "                                 ),\n",
    "        # 视觉映射配置项，用于进行视觉编码，也就是数据映射到视觉元素上\n",
    "        visualmap_opts=opts.VisualMapOpts(is_show=False, # 不显示视觉映射组件\n",
    "                                          max_=215, # 视觉映射的最大值，科学计数法\n",
    "                                          is_piecewise=False, # 是否为分段型视觉映射\n",
    "                                          dimension=0, # 指定视觉映射的维度，这里是第一个维度(通常指数据的第一列)\n",
    "                                          range_color=['rgba(219,112,147,0.4)','rgba(238,25,27,1)'] # 视觉映射的颜色范围\n",
    "                                         )\n",
    "    )\n",
    "    .reversal_axis() # 反转轴，将柱形图变为水平柱形图\n",
    ")\n",
    "\n",
    "# 创建grid对象，相当于通过grid进行度世界地图和水平柱形图的布局\n",
    "grid = (\n",
    "    Grid(init_opts=opts.InitOpts(theme='dark',width='1000px'))\n",
    "    .add(bar,grid_opts=opts.GridOpts(pos_top='60%',pos_right='70%',pos_left='5%'))\n",
    "    .add(geo,grid_opts=opts.GridOpts(pos_bottom='12%'))\n",
    ")\n",
    "\n",
    "grid.render('geo-bar.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5afe099-3b2a-4bee-bf4d-46bf4642f0c3",
   "metadata": {},
   "source": [
    "3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "53cd12cf-c1de-41d8-ac9a-627daa3ef0a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('北京', '北京'), ('北京', '天津'), ('北京', '河北'), ('北京', '山西'), ('北京', '内蒙古'), ('北京', '辽宁'), ('北京', '吉林'), ('北京', '黑龙江'), ('北京', '上海'), ('北京', '江苏'), ('北京', '浙江'), ('北京', '安徽'), ('北京', '福建'), ('北京', '江西'), ('北京', '山东'), ('北京', '河南'), ('北京', '湖北'), ('北京', '湖南'), ('北京', '广东'), ('北京', '广西'), ('北京', '海南'), ('北京', '重庆'), ('北京', '四川'), ('北京', '贵州'), ('北京', '云南'), ('北京', '西藏'), ('北京', '陕西'), ('北京', '甘肃'), ('北京', '青海'), ('北京', '宁夏'), ('北京', '新疆'), ('北京', '香港'), ('北京', '澳门'), ('北京', '台湾')]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\geo-map.html'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Geo,Bar,Grid \n",
    "from pyecharts.globals import ChartType,SymbolType\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "from pyecharts.commons.utils import JsCode\n",
    "import random\n",
    "\n",
    "# 中国34个省市自治区名称列表\n",
    "provinces = [\n",
    "    \"北京\",\"天津\",\"河北\",\"山西\",\"内蒙古\",\"辽宁\",\"吉林\",\"黑龙江\",\"上海\",\n",
    "    \"江苏\",\"浙江\",\"安徽\",\"福建\",\"江西\",\"山东\",\"河南\",\"湖北\",\"湖南\",\n",
    "    \"广东\",\"广西\",\"海南\",\"重庆\",\"四川\",\"贵州\",\"云南\",\"西藏\",\"陕西\",\n",
    "    \"甘肃\",\"青海\",\"宁夏\",\"新疆\",\"香港\",\"澳门\",\"台湾\"\n",
    "]\n",
    "# 使用字典推导式构建字典，每个地区分配一个1-200之间的随机数值\n",
    "province_data = {province:random.randint(1,200) for province in provinces}\n",
    "data_pair = [[k,v] for k,v in province_data.items()]\n",
    "dp = [('北京',i) for i in province_data]\n",
    "print(dp)\n",
    "'''\n",
    "第一行：这行代码使用字典推导式来创建一个名为province_data的字典。它遍历provinces列表（包含中国34个省市自治区的名称），\n",
    "并将每个省份的名称作为键（province），为每个键分配一个1到200之间的随机整数作为值（通过random.randint(1, 200)生成）\n",
    "第二行：这行代码使用列表推导式将province_data字典中的项（键值对）转换成一个列表。province_data.items()方法返回一个\n",
    "包含所有键值对的视图对象，每个键值对都是一个元组。列表推导式遍历这些元组，并将每个元组转换成一个包含两个元素的列表（即键和值）\n",
    "第三行：dp = [('北京', i) for i in province_data]\n",
    "\n",
    "'''\n",
    "\n",
    "itemstyle = {\n",
    "    'normal':{\n",
    "        'borderWidth':1,\n",
    "        'borderColor':JsCode(\"\"\"new echarts.graphic.LinearGradient(0,0,1,1,[\n",
    "        {offset:0, color:'rgb(255,191,0)'},\n",
    "        {offset:1, color:'rgb(224,62,76)'}\n",
    "        ])\n",
    "        \"\"\"),\n",
    "        'shadowColor':'green',\n",
    "        'shadowBlur':1,\n",
    "        'areaColor':'#2b3441',\n",
    "    }\n",
    "}\n",
    "lc = JsCode(\"\"\"new echarts.graphic.LinearGradient(0,0,1,1,[\n",
    "        {offset:0, color:'rgb(255,191,0)'},\n",
    "        {offset:0.5, color:'rgb(255,255,255)'},\n",
    "        {offset:1, color:'rgb(224,62,76)'}\n",
    "        ])\n",
    "        \"\"\")\n",
    "lcj = JsCode(\"\"\"new echarts.graphic.LinearGradient(0,0,0,1,[\n",
    "        {offset:0, color:'#58B3CC'},\n",
    "        {offset:1, color:'#F58158'}\n",
    "        ])\n",
    "        \"\"\")\n",
    "geo = (Geo(init_opts=opts.InitOpts(theme=ThemeType.CHALK,width='100%',height='100vh'))\n",
    "       .add_schema(maptype='china',# 地图的类型\n",
    "                  zoom=1.5, # 地图的大小\n",
    "                  center=[110,35], # 视角中心\n",
    "                  label_opts=opts.LabelOpts(color='white',font_weight='bold',font_size=12),\n",
    "                  itemstyle_opts=itemstyle)\n",
    "       .add(\"\",dp,type_=ChartType.LINES,  # 设置线的样式\n",
    "           symbol_size=3, # 线条末点的大小\n",
    "           linestyle_opts=opts.LineStyleOpts(color=lcj,curve=2,width=2), # 线条的样式\n",
    "           effect_opts=opts.EffectOpts(  # 发散出去的点的样式配置\n",
    "               symbol=SymbolType.ARROW,symbol_size=7,color='cyan',trail_length=.3\n",
    "           ))\n",
    "      )\n",
    "geo.render('geo-map.html')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
